<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="基地海拔" prop="baseAltitude">
              <el-input v-model="queryParams.baseAltitude" placeholder="请输入基地海拔" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="排序" prop="orderNum">
              <el-input v-model="queryParams.orderNum" placeholder="请输入排序" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="基地关联部门id" prop="deptId">
              <el-input v-model="queryParams.deptId" placeholder="请输入基地关联部门id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['agriculture:baseinfo:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['agriculture:baseinfo:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['agriculture:baseinfo:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['agriculture:baseinfo:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-able="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="baseinfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="基地ID" align="center" prop="baseId" />
        <el-table-column label="基地简称" align="center" prop="baseShortName" />
        <el-table-column label="基地编号" align="center" prop="baseCode" />
        <el-table-column label="基地全称" align="center" prop="baseName" />
        <el-table-column label="基地负责人" align="center" prop="baseLeader" />
        <el-table-column label="基地负责人电话" align="center" prop="leaderTel" />
        <el-table-column label="基地负责人地址" align="center" prop="baseAddress" />
        <el-table-column label="基地面积" align="center" prop="baseArea" />
        <el-table-column label="基地海拔" align="center" prop="baseAltitude" />
        <el-table-column label="现场图片" align="center" prop="baseImg" />
        <el-table-column label="基地描述" align="center" prop="baseDes" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="状态" align="center" prop="status" />
        <el-table-column label="排序" align="center" prop="orderNum" />
        <el-table-column label="${comment}" align="center" prop="baseCoordinate" />
        <el-table-column label="基地关联部门id" align="center" prop="deptId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['agriculture:baseinfo:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['agriculture:baseinfo:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改基地信息对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="baseinfoFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="基地海拔" prop="baseAltitude">
          <el-input v-model="form.baseAltitude" placeholder="请输入基地海拔" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="基地关联部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入基地关联部门id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Baseinfo" lang="ts">
import { listBaseinfo, getBaseinfo, delBaseinfo, addBaseinfo, updateBaseinfo } from '@/api/agriculture/baseinfo';
import { BaseinfoVO, BaseinfoQuery, BaseinfoForm } from '@/api/agriculture/baseinfo/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const baseinfoList = ref<BaseinfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const baseinfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BaseinfoForm = {
  baseId: undefined,
  baseShortName: undefined,
  baseCode: undefined,
  baseName: undefined,
  baseLeader: undefined,
  leaderTel: undefined,
  baseAddress: undefined,
  baseArea: undefined,
  baseAltitude: undefined,
  baseImg: undefined,
  baseDes: undefined,
  remark: undefined,
  status: undefined,
  orderNum: undefined,
  baseCoordinate: undefined,
  deptId: undefined
};
const data = reactive<PageData<BaseinfoForm, BaseinfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    baseShortName: undefined,
    baseCode: undefined,
    baseName: undefined,
    baseLeader: undefined,
    leaderTel: undefined,
    baseAddress: undefined,
    baseArea: undefined,
    baseAltitude: undefined,
    baseImg: undefined,
    baseDes: undefined,
    status: undefined,
    orderNum: undefined,
    baseCoordinate: undefined,
    deptId: undefined,
    params: {}
  },
  rules: {
    baseId: [{ required: true, message: '基地ID不能为空', trigger: 'blur' }],
    baseShortName: [{ required: true, message: '基地简称不能为空', trigger: 'blur' }],
    baseCode: [{ required: true, message: '基地编号不能为空', trigger: 'blur' }],
    baseName: [{ required: true, message: '基地全称不能为空', trigger: 'blur' }],
    baseLeader: [{ required: true, message: '基地负责人不能为空', trigger: 'blur' }],
    leaderTel: [{ required: true, message: '基地负责人电话不能为空', trigger: 'blur' }],
    baseAddress: [{ required: true, message: '基地负责人地址不能为空', trigger: 'blur' }],
    baseArea: [{ required: true, message: '基地面积不能为空', trigger: 'blur' }],
    baseAltitude: [{ required: true, message: '基地海拔不能为空', trigger: 'blur' }],
    baseImg: [{ required: true, message: '现场图片不能为空', trigger: 'blur' }],
    baseDes: [{ required: true, message: '基地描述不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    orderNum: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
    baseCoordinate: [{ required: true, message: '$comment不能为空', trigger: 'blur' }],
    deptId: [{ required: true, message: '基地关联部门id不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询基地信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBaseinfo(queryParams.value);
  baseinfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  baseinfoFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: BaseinfoVO[]) => {
  ids.value = selection.map((item) => item.baseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加基地信息';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: BaseinfoVO) => {
  reset();
  const _baseId = row?.baseId || ids.value[0];
  const res = await getBaseinfo(_baseId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改基地信息';
};

/** 提交按钮 */
const submitForm = () => {
  baseinfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.baseId) {
        await updateBaseinfo(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addBaseinfo(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: BaseinfoVO) => {
  const _baseIds = row?.baseId || ids.value;
  await proxy?.$modal.confirm('是否确认删除基地信息编号为"' + _baseIds + '"的数据项？').finally(() => (loading.value = false));
  await delBaseinfo(_baseIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'agriculture/baseinfo/export',
    {
      ...queryParams.value
    },
    `baseinfo_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
